package com.zanglikun.apacheCommonUtil.apacheCommonsLang3;

import com.zanglikun.apacheCommonUtil.pojo.Pojo;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

/**
 * @author : zanglikun
 * @date : 2024/9/1 下午6:12
 * @desc : Copyright © zanglikun.com
 * 反射工具类，快速获得对象的属性。
 */
public class FieldUtilsTest {
    public static void main(String[] args) {
        Pojo pojo = new Pojo();
        pojo.setName("大哥");
        try {
            Object name = FieldUtils.readField(pojo, "name", true); // 获取任意对象的属性值
            System.out.println("获得的属性值是：" + name);

            Field[] allFields = FieldUtils.getAllFields(Pojo.class);
            for (Field field : allFields) {
                Type genericType = field.getGenericType();
                System.out.println(field.getName() + " 的类型是 " + genericType.getTypeName());
            }

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }
}
